package eSchool.GUI.Controls;

import eSchool.BL.CriteriosPesquisa;
import eSchool.GUI.Components.IUIConector;
import eSchool.GUI.Components.UIConectorUtilizador;
import eSchool.Utils.TextHelper;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.TableModel;

public class panTabUtilizador extends JPanelExtended {
  
    public panTabUtilizador() {
        initComponents();

        panDataGrid1.setEventListner(this);
        panDetalheUtilizador1.setEventListner(this);
 }

  @Override
    public void SaveButtonClickEvent(Object Sender, Object obj) {
        uiConector.gravaDetalhe(panDetalheUtilizador1.getDataSource());
    }
        
    @Override
    public void GridDoubleClickEvent(Object sender, Object obj) {
        try {
            DataGrid dg = (DataGrid) sender;
                       
            int row = ((Integer) obj).intValue();
            
            String num = TextHelper.EmptyString;
            if(dg.getValueAt(row, 0) != null)
                num = dg.getValueAt(row, 0).toString();

            CriteriosPesquisa cr = new CriteriosPesquisa();
            cr.add("num",num);
            
            TableModel tableModel = uiConector.pesquisaDetalhe(cr);
            
            this.panDetalheUtilizador1.setDataSource(tableModel);
            this.setSelectedTab(1);
        
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Não foi possível identificar o objecto detalhe. \n" + ex.getMessage()); 
            Logger.getLogger(panTabUtilizador.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void SearchButtonClickEvent(Object Sender, Object obj) {
        try {
            
            CriteriosPesquisa cr = (CriteriosPesquisa)obj;
            
            this.panDataGrid1.setDataSource(uiConector.pesquisaLista(cr));
            this.panDataGrid1.DataBind();

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Não foi possível obter o objecto de pesquisa. \n" + ex.getMessage()); 
            Logger.getLogger(panDataGrid.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void setSelectedTab(int index){
            this.tabConsulta.setSelectedIndex(index);
    }
   
    public void setObjectoTrabalho(UIConectorUtilizador uiConector){
        try {
            this.uiConector = uiConector;
            this.panDetalheUtilizador1.setDataSource(this.uiConector.getDetalhe());
            DataGridColumnIdentifiers dcs = new DataGridColumnIdentifiers();
           
            dcs.add("Utilizador", "codUtilizador", 50);
            dcs.add("Palavra Chave", "palavraChave", 50);
            dcs.add("Grupo", "codGrupoUtilizador", 50);
            this.panDataGrid1.setDataGridColumnIdentifiers(dcs);
            this.panDataGrid1.setDataSource(this.uiConector.getLista());
            this.panDataGrid1.DataBind();
                
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Não foi possível obter o objecto. \n" + ex.getMessage()); 
            Logger.getLogger(panTabUtilizador.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
  
        
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabConsulta = new javax.swing.JTabbedPane();
        pnlPesquisa = new javax.swing.JPanel();
        panDataGrid1 = new eSchool.GUI.Controls.panDataGrid();
        pnlDetalhe = new javax.swing.JPanel();
        panDetalheUtilizador1 = new eSchool.GUI.Controls.panDetalheUtilizador();

        javax.swing.GroupLayout pnlPesquisaLayout = new javax.swing.GroupLayout(pnlPesquisa);
        pnlPesquisa.setLayout(pnlPesquisaLayout);
        pnlPesquisaLayout.setHorizontalGroup(
            pnlPesquisaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPesquisaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panDataGrid1, javax.swing.GroupLayout.DEFAULT_SIZE, 636, Short.MAX_VALUE)
                .addContainerGap())
        );
        pnlPesquisaLayout.setVerticalGroup(
            pnlPesquisaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlPesquisaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panDataGrid1, javax.swing.GroupLayout.DEFAULT_SIZE, 379, Short.MAX_VALUE)
                .addContainerGap())
        );

        tabConsulta.addTab("Pesquisa", pnlPesquisa);

        javax.swing.GroupLayout pnlDetalheLayout = new javax.swing.GroupLayout(pnlDetalhe);
        pnlDetalhe.setLayout(pnlDetalheLayout);
        pnlDetalheLayout.setHorizontalGroup(
            pnlDetalheLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDetalheLayout.createSequentialGroup()
                .addComponent(panDetalheUtilizador1, javax.swing.GroupLayout.DEFAULT_SIZE, 646, Short.MAX_VALUE)
                .addContainerGap())
        );
        pnlDetalheLayout.setVerticalGroup(
            pnlDetalheLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDetalheLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panDetalheUtilizador1, javax.swing.GroupLayout.DEFAULT_SIZE, 379, Short.MAX_VALUE)
                .addContainerGap())
        );

        tabConsulta.addTab("Detalhe", pnlDetalhe);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tabConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, 661, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(tabConsulta, javax.swing.GroupLayout.DEFAULT_SIZE, 426, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    
    // <editor-fold defaultstate="collapsed" desc="Declaração de variáveis">                          
    
    private IUIConector uiConector;
     
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private eSchool.GUI.Controls.panDataGrid panDataGrid1;
    private eSchool.GUI.Controls.panDetalheUtilizador panDetalheUtilizador1;
    private javax.swing.JPanel pnlDetalhe;
    private javax.swing.JPanel pnlPesquisa;
    private javax.swing.JTabbedPane tabConsulta;
    // End of variables declaration//GEN-END:variables
    
    // </editor-fold>
}
